package com.zt.service;

import com.zt.mapper.SetmealMapper;
import com.zt.pojo.Setmeal;
import com.zt.util.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class RecvServiceImpl implements RecvService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private RedisClient redisClient;

    @Override
    public void add(Setmeal setmeal) {
        setmealMapper.add(setmeal);
    }

    @Override
    public void delete(Integer id) {
        setmealMapper.delete(id);
    }

    @Override
    public void update(Setmeal setmeal1) {
        setmealMapper.update(setmeal1);
        redisClient.set("setmeal_list" + setmeal1.getId(), setmeal1);
    }

    @Override
    public Setmeal get(Integer id) {
        Setmeal setmeal = (Setmeal) redisClient.get("setmeal_list" + id);
        if (setmeal != null) {
            return setmeal;
        }
        if (redisClient.setnx("setmeal_list" + id, id)) {
            try {
                setmeal = setmealMapper.get(id);
//解决缓存穿透,设置空对象
                if (setmeal == null) {
                    setmeal = new Setmeal();
                    redisClient.expire("setmeal_list" + id, 30);
                }
                redisClient.set("setmeal_list" + id, setmeal);
                redisClient.expire("setmeal_list" + id,30);
                return setmeal;
            } finally {
                redisClient.del("setmeal_list" + id);
            }
        } else {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return get(id);
        }
    }

    @Override
    public List<Setmeal> page() {
        List<Object> Setmeallist = redisClient.lrange("setmeal_page", 0, -1);
        List<Setmeal> list = new ArrayList<>();
        if (Setmeallist != null && Setmeallist.size() > 0) {
            for (Object o : Setmeallist) {
                Setmeal setmeal = (Setmeal) o;
                list.add(setmeal);
            }
            return list;
        }
        list = setmealMapper.page();
        for (Setmeal setmeal : list) {
            redisClient.rpush("setmeal_page", setmeal);
        }
        return list;
    }

    @Override
    public List<Setmeal> like() {
        List<Object> setmealList = redisClient.lrange("setmeal_like", 0, -1);
        List<Setmeal> list = new ArrayList<>();
        if (setmealList != null && setmealList.size() > 0) {
            for (Object o : setmealList) {
                Setmeal setmeal = (Setmeal) o;
                list.add(setmeal);
                return list;
            }
        }
        list = setmealMapper.like();
        for (Setmeal setmeal : list) {
            redisClient.rpush("setmeal_like", setmeal);
        }
        return list;
    }

    @Override
    public List<Setmeal> between(String name, Integer min, Integer max) {
        return setmealMapper.between(name, min, max);
    }
}
